<template>
  <el-dialog :title="this.type==='add'?'新增自习室类型':'修改自习室类型'" :visible.sync="activeTopTemp" :close-on-click-modal="false" @close="dialogFormVisible=false">
    <el-form :model="formObj" ref="formObj" :rules="rules" >
      <el-form-item label="自习室类型ID">
        <el-input v-model="formObj.id" :disabled="true" placeholder="新增自习室类型时系统分配ID,不可指定"></el-input>
      </el-form-item>
      <el-form-item label="自习室类型名称" prop="typeName">
        <el-input v-model="formObj.typeName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="类型简介" prop="typeDesc">
        <el-input v-model="formObj.typeDesc" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="单位时间" prop="unitType">
        <el-select v-model="formObj.unitType" placeholder="自习室类型">
          <el-option v-for="item in allUnitType" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位价格" prop="unitPrice">
        <el-input-number v-model="formObj.unitPrice" :precision="2" :step="1"  :min="0.0"></el-input-number>
      </el-form-item>
      <el-form-item label="初始价格" prop="startPrice" style="width: 40%">
        <el-input-number v-model="formObj.startPrice" :precision="2" :step="1"  :min="0.0"></el-input-number>
      </el-form-item>
      <el-form-item label="封顶价格" prop="maxPrice" >
        <el-input-number v-model="formObj.maxPrice" :precision="2" :step="1"  :min="0.0"></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {doAddRoomType, doGetAllUnitType, doUpdateRoomType} from "@/api/roomType";

export default {
  name: "roomTypeDialog",
  props:{
    type:String,
    dialogFormVisible:Boolean,
    roomType:Object
  },
  data() {
    return{
      activeTopTemp:this.dialogFormVisible,
      formObj:this.roomType,
      rules: {
        typeName:[ { required: true, message: '请输入自习室类型名称', trigger: 'blur' }],
        startPrice:[ { required: true, message: '请输入初始价格', trigger: 'blur' }],
        maxPrice:[ { required: true, message: '请选择封顶价格', trigger: 'blur' }],
        unitType:[ { required: true, message: '请填写单位时间类型', trigger: 'blur' }],
        unitPrice:[ { required: true, message: '请选择单位价格', trigger: 'blur' }],
        typeDesc:[ { required: true, message: '请填写自习室类型简介', trigger: 'blur' }]
      },
      allUnitType: []
    }
  },
  methods:{
    onSubmit() {
      //参数校验
      this.$refs.formObj.validate((valid) => {
        if (valid) {
          if (this.type==='add'){
            doAddRoomType(this.formObj).then(res=>{
              if (res.code===200){
                this.activeTopTemp=false;
                this.$emit('change',true)
              }
            })
          }else{
            doUpdateRoomType(this.formObj).then(res=>{
              if (res.code===200){
                this.activeTopTemp=false;
                this.$emit('change',true)
              }
            })
          }
        } else {
          return false;
        }
      })
    }
  },watch:{
    dialogFormVisible(){
      this.activeTopTemp=this.dialogFormVisible;
    },
    roomType(){
      this.formObj = this.roomType;
    }
  },
  mounted() {
    doGetAllUnitType().then(res=>{
      if (res.code===200){
        this.allUnitType = res.data;
      }
    })
  }
}
</script>

<style scoped>

</style>